package com.qumitech.visualnovel.controller.rest;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.model.common.BaseResponse;
import com.qumitech.visualnovel.model.dao.console.AccountLogin;
import com.qumitech.visualnovel.model.dao.console.RequestExceptInfo;
import com.qumitech.visualnovel.service.forbidden.UserForbiddenService;
import com.qumitech.visualnovel.service.requests.RequestExceptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * console_backend
 *
 * @author zhangbc
 * @version 1.0.0
 * @date 2020/7/18 17:28
 **/
@RestController
@RequestMapping("/api/player")
public class RequestExceptController {
    @Autowired
    private RequestExceptService requestExceptService;

    @Autowired
    private UserForbiddenService userForbiddenService;

    @RequestMapping(value = "/error_requests/list", method = RequestMethod.GET)
    public JSONObject listRequestExcept(@RequestParam(value="uid",required=false) Integer accountId,
                                        @RequestParam(value="startTime",required=false) String startTime,
                                        @RequestParam(value="endTime",required=false) String endTime,
                                        @RequestParam(value="phone",required=false) String phone,
                                        @RequestParam(value="offset", required=false, defaultValue="0") int offset,
                                        @RequestParam(value="limit", required=false, defaultValue="20") int limit,
                                        @RequestParam(value="orderType", required=false, defaultValue="DESC") String orderType,
                                        @RequestParam(value="orderBy", required=false, defaultValue="error_time") String orderBy)
    {

        List<RequestExceptInfo> requestExceptList = requestExceptService.getRequestExceptList(accountId, phone,
                startTime, endTime, offset, limit, orderType, orderBy);
        JSONObject data = new JSONObject();
        JSONArray dataItems = new JSONArray();
        data.put("count", requestExceptService.getRequestExceptCount(accountId, phone, startTime, endTime));
        data.put("items", dataItems);
        Set<String> ipSet = new HashSet<>();
        Set<String> deviceSet = new HashSet<>();
        if(requestExceptList != null)
        {
            for(RequestExceptInfo item: requestExceptList)
            {
                JSONObject requestItem = new JSONObject();
                requestItem.put("ipAddress", item.getIpAddress());
                requestItem.put("ipForbiddenStatus", 0);
                requestItem.put("deviceId", item.getDeviceId());
                requestItem.put("deviceIdForbiddenStatus", 0);
                requestItem.put("accountId", item.getAccountId());
                requestItem.put("phone", item.getPhone());
                requestItem.put("phoneName", item.getPhoneName());
                requestItem.put("reqType", item.getReqType());
                requestItem.put("appId", item.getAppId());
                requestItem.put("appChannel", item.getAppChannel());
                requestItem.put("appName", item.getAppName());
                requestItem.put("appVersion", item.getAppVersion());
                requestItem.put("errorTime", item.getErrorTime());
                requestItem.put("errorDesc", item.getErrorDesc());
                ipSet.add(item.getIpAddress());
                deviceSet.add(item.getDeviceId());
                dataItems.add(requestItem);
            }
        }
        ipSet = userForbiddenService.findForbiddenIp(ipSet);
        deviceSet = userForbiddenService.findForbiddenDeviceId(deviceSet);
        for(int i = 0; i < dataItems.size(); i++)
        {
            JSONObject requestItem = dataItems.getJSONObject(i);
            if (ipSet.contains(requestItem.getString("ipAddress")))
            {
                requestItem.put("ipForbiddenStatus", 1);
            }
            if (deviceSet.contains(requestItem.getString("deviceId")))
            {
                requestItem.put("deviceIdForbiddenStatus", 1);
            }
        }

        return new BaseResponse(data);
    }
}
